﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace WebChat
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.NavigationMenuChatters.Visible = false;
            this.NavigationMenuAdmins.Visible = false;

            var membershipUser = Membership.GetUser();
            if (membershipUser != null)
            {
                WebChatDAL.WebChatDAO dao =new WebChatDAL.WebChatDAO();

                WebChatDAL.Chatter chatter;
                if (dao.TryGetChatterByUserName(membershipUser.UserName, out chatter))
                {
                    chatter.aspnet_Users.aspnet_Membership.LastLoginDate = DateTime.Now;
                    dao.SaveChanges();

                    bool isAdmin = false;

                    foreach (var role in chatter.aspnet_Users.aspnet_Roles)
                    {
                        if (role.RoleName == "Admins")
                        {
                            isAdmin = true;
                        }
                    }

                    if (isAdmin)
                    {
                        this.NavigationMenuAdmins.Visible = true;                        
                    }
                    else
                    {
                        this.NavigationMenuChatters.Visible = true;
                    }
                }                
            }
        }
    }
}
